package com.bishe.demo.modules.other.service.impl;

import com.bishe.demo.modules.common.entity.ResultEntity;
import com.bishe.demo.modules.common.entity.SearchBean;
import com.bishe.demo.modules.other.dao.*;
import com.bishe.demo.modules.other.entity.Image;
import com.bishe.demo.modules.other.entity.Scenery;
import com.bishe.demo.modules.other.entity.SceneryImage;
import com.bishe.demo.modules.other.entity.SceneryUser;
import com.bishe.demo.modules.other.service.SceneryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

@Service
public class SceneryServiceImpl implements SceneryService {
    @Autowired
    private SceneryDao sceneryDao;

    @Autowired
    private SceneryImageDao sceneryImageDao;

    @Autowired
    private SceneryMessageDao sceneryMessageDao;

    @Autowired
    private SceneryTravelDao sceneryTravelDao;

    @Autowired
    private SceneryUserDao sceneryUserDao;

    @Autowired
    private ImageDao imageDao;

    @Override
    @Transactional
    public ResultEntity<Scenery> insertScenery(Scenery scenery) {
        sceneryDao.insertScenery(scenery);
        sceneryUserDao.insertSceneryUser(new SceneryUser(scenery.getSceneryId(), scenery.getUser().getUserId()));
        List<Image> images = scenery.getImages();
        if (images != null && images.size() > 0) {
            for (Image image : images) {
                imageDao.insertImage(image);
                sceneryImageDao.insertSceneryImage(new SceneryImage(scenery.getSceneryId(), image.getImageId()));
            }
        }
        return new ResultEntity<Scenery>(ResultEntity.ResultStatus.SUCCESS.code, "添加成功", scenery);
    }

    @Override
    @Transactional
    public ResultEntity<Scenery> updateScenery(Scenery scenery) {
        sceneryDao.updateScenery(scenery);
        List<Image> images = scenery.getImages();
        if (images != null && images.size() > 0) {
            sceneryImageDao.deleteSceneryImageBySceneryId(scenery.getSceneryId());
            for (Image image : images) {
                sceneryImageDao.insertSceneryImage(new SceneryImage(scenery.getSceneryId(), image.getImageId()));
            }
        }
        return new ResultEntity<Scenery>(ResultEntity.ResultStatus.SUCCESS.code, "修改成功", scenery);
    }

    @Override
    @Transactional
    public ResultEntity<Scenery> deleteSceneryBySceneryId(int sceneryId) {
        List<Integer> ids=sceneryImageDao.getImagesIds(sceneryId);
        for (Integer id : ids) {
            imageDao.deleteImageByImageId(id);
        }
        sceneryDao.deleteSceneryBySceneryId(sceneryId);
        sceneryImageDao.deleteSceneryImageBySceneryId(sceneryId);
        sceneryUserDao.deleteSceneryUserBySceneryId(sceneryId);
        return new ResultEntity<Scenery>(ResultEntity.ResultStatus.SUCCESS.code, "删除成功");
    }

    @Override
    public Scenery getSceneryBySceneryId(int sceneryId) {
        return sceneryDao.getSceneryBySceneryId(sceneryId);
    }

    @Override
    public PageInfo<Scenery> getSceneryBySearchBean(SearchBean searchBean) {
        searchBean.initSearchBean();
        PageHelper.startPage(searchBean.getCurrentPage(), searchBean.getPageSize());
        return new PageInfo<Scenery>(Optional
                .ofNullable(sceneryDao.getSceneryBySearchBean(searchBean))
                .orElse(Collections.emptyList()));
    }
}
